package web.admin;

import application.exportimport.ContentInventoryMaker;
import application.exportimport.DatabaseExporter;
import application.exportimport.PathIsNotSetException;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import web.admin.cms.CmsAdmin;
import web.admin.config.ConfigurationLeftMenu;
import web.admin.config.ConfigurationPanel;
import web.admin.shop.DeliveryMethodAdminPanel;
import web.admin.shop.ImportPanel;
import web.admin.shop.PayMethodAdminPanel;
import web.admin.shop.category.CategoryAdminPanel;
import web.admin.shop.category.CategoryLeftMenuAdminPanel;
import web.admin.shop.product.ProductAdminPanel;
import web.admin.shop.product.ProductSearchPanel;
import web.admin.user.UserAdminPanel;

import javax.xml.bind.JAXBException;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

public final class AdminMenuPanel extends Panel {

    @SpringBean
    DatabaseExporter exportDB;
    @SpringBean
    ContentInventoryMaker contentInventoryMaker;
    private static Logger logger = Logger.getLogger(AdminMenuPanel.class.getName());

    //  do li pridat  class="active"
    //AttributeModifier modifier = new AttributeModifier("class", new Model("active"));
    public AdminMenuPanel(String id, final AdminPage parent) {
        super(id);
        add(new Link("pages") {

            @Override
            public void onClick() {

                CmsAdmin pageEditAdminPanel = new CmsAdmin("current");
                parent.setCurrentPanelTo(pageEditAdminPanel);
                parent.setLeftPanelToEmpty();

            }
        });

        add(new Link("categories") {

            @Override
            public void onClick() {
                CategoryAdminPanel categoryAdminPanel = new CategoryAdminPanel("current", parent);
                parent.setCurrentPanelTo(categoryAdminPanel);
                parent.setLeftPanel(new CategoryLeftMenuAdminPanel("left", parent, categoryAdminPanel));
            }
        });

        add(new Link("products") {

            @Override
            public void onClick() {
                ProductAdminPanel productAdminPanel = new ProductAdminPanel("current");
                parent.setCurrentPanelTo(productAdminPanel);
                parent.setLeftPanel(new ProductSearchPanel("left", parent, productAdminPanel));
            }
        });

        add(new Link("users") {

            @Override
            public void onClick() {
                UserAdminPanel userAdminPanel = new UserAdminPanel("current");
                parent.setCurrentPanelTo(userAdminPanel);
                parent.setLeftPanelToEmpty();

            }
        });


        add(new Link("config") {

            @Override
            public void onClick() {
                ConfigurationPanel configurationPanel = new ConfigurationPanel("current");
                parent.setCurrentPanelTo(configurationPanel);
                parent.setLeftPanel(new ConfigurationLeftMenu("left", parent));

            }
        });

        add(new Link("manageDeliveryMethod") {

            @Override
            public void onClick() {
                DeliveryMethodAdminPanel deliveryMethodAdminPanel = new DeliveryMethodAdminPanel("current");
                parent.setCurrentPanelTo(deliveryMethodAdminPanel);
                parent.setLeftPanelToEmpty();

            }
        });

        add(new Link("managePayMethod") {

            @Override
            public void onClick() {
                PayMethodAdminPanel payMethodAdminPanel = new PayMethodAdminPanel("current");
                parent.setCurrentPanelTo(payMethodAdminPanel);
                parent.setLeftPanelToEmpty();

            }
        });
        add(new Link("upload") {

            @Override
            public void onClick() {
                //TODO
//                UploadPanel panel = new UploadPanel("current");
//                parent.setCurrentPanelTo(panel);
//                parent.setLeftPanelToEmpty();

            }
        });

        add(new Link("exportZboziCz") {

            @Override
            public void onClick() {
                parent.setCurrentPanelToEmpty();
                try {
                    exportDB.exportDB();
                    info(
                            getLocalizer().getString("export.success.zbozicz", parent));
                } catch (FileNotFoundException ex) {
                    error(
                            getLocalizer().getString("export.fail.zbozicz.fileNotFound", parent));
                    logger.log(Level.SEVERE, null, ex);
                } catch (JAXBException ex) {
                    error(
                            getLocalizer().getString("export.fail.zbozicz.io", parent));
                    logger.log(Level.SEVERE, null, ex);
                } catch (PathIsNotSetException ex) {
                    error(
                            getLocalizer().getString("export.fail.zbozicz.pathIsNotSet", parent));
                    logger.log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                    error(
                            getLocalizer().getString("export.fail.zbozicz.io", parent));
                    logger.log(Level.SEVERE, null, ex);
                }
            }
        });

        add(new Link("exportContentInventory") {

            @Override
            public void onClick() {
                parent.setCurrentPanelToEmpty();
                try {
                    contentInventoryMaker.generateContentInventory();
                    info(
                            getLocalizer().getString("export.success.contentInventory", parent));
                } catch (PathIsNotSetException ex) {
                    error(
                            getLocalizer().getString("export.fail.contentinventory.pathIsNotSet", parent));
                    logger.log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                    error(
                            getLocalizer().getString("export.fail.contentinventory.io", parent));
                    logger.log(Level.SEVERE, null, ex);
                }
            }
        });
        //TODO web import :]
        add(new Link("import") {

            @Override
            public void onClick() {
                ImportPanel panel = new ImportPanel("current");
                parent.setCurrentPanelTo(panel);
                parent.setLeftPanelToEmpty();
            }
        });
    }
}
